package com.market.service.admin.impl;

import com.market.bean.*;
import com.market.mapper.MarketPermissionMapper;
import com.market.mapper.MarketRoleMapper;
import com.market.service.admin.AdminAuthService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author fan
 * @Description:
 * @since 2023/01/02 14:25
 */
@Service
public class AdminAuthServiceImpl implements AdminAuthService {
    @Autowired
    MarketPermissionMapper permissionMapper;
    @Autowired
    MarketRoleMapper roleMapper;

    @Override
    public InfoData info(MarketAdmin marketAdmin) {
        InfoData data = new InfoData();
        data.setAvatar(marketAdmin.getAvatar());
        data.setName(marketAdmin.getUsername());

        MarketPermissionExample permissionExample = new MarketPermissionExample();
        MarketPermissionExample.Criteria criteria = permissionExample.createCriteria();
        criteria.andRoleIdIn(Arrays.asList(marketAdmin.getRoleIds()));
        criteria.andDeletedEqualTo(false);
        List<MarketPermission> marketPermissions = permissionMapper.selectByExample(permissionExample);
        ArrayList<String> permissions = new ArrayList<>();
        for (MarketPermission marketPermission : marketPermissions) {
            permissions.add(marketPermission.getPermission());
        }
        data.setPerms(permissions);

        MarketRoleExample roleExample = new MarketRoleExample();
        MarketRoleExample.Criteria criteria1 = roleExample.createCriteria();
        criteria1.andDeletedEqualTo(false);
        criteria1.andIdIn(Arrays.asList(marketAdmin.getRoleIds()));
        List<MarketRole> marketRoles = roleMapper.selectByExample(roleExample);
        ArrayList<String> name = new ArrayList<>();
        for (MarketRole marketRole : marketRoles) {
            name.add(marketRole.getName());
        }
        data.setRoles(name);

        return data;
    }
}
